package com.xiaocq.wordskiller;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class ResultShow extends Activity{
	private static final String TAG = "ResultShow";
	
	private Button backToMenu;
	private TextView showAllTime,showAllCounts,showHitRate,showRate;
	private ListView wrongSet;
	
	private long latelyAccessTimeInMillis;
	private long allTimeInMillis;
	private int wrongCounts;
	private int allCounts;
	private double ratePer;
	private Cursor cursor;
	
	private ProgressDialog progressDialog;
	
	private MyAdapter adapter;
	
	private Handler loadingHandler = new Handler(){
		public void handleMessage(android.os.Message msg) {
			wrongSet.setAdapter(adapter);
			progressDialog.dismiss();
		};
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.resultshow);
		
		Intent intent = getIntent();
		loadFromIntent(intent);
		
		initComponents();
		
		prepareInBackground();
	}
	
	private void loadFromIntent(Intent intent){
		latelyAccessTimeInMillis = intent.getLongExtra(CommandList.LATELY_ACCESS, 0L);
		allTimeInMillis = intent.getLongExtra(CommandList.ALL_TIME, 0L);
		wrongCounts = intent.getIntExtra(CommandList.WRONG_COUNTS, 0);
		allCounts = intent.getIntExtra(CommandList.ALL_COUNTS, 0);
		
	}
	
	private void initComponents(){
		backToMenu = (Button) findViewById(R.id.result_show_backToMenu);
		showAllTime = (TextView) findViewById(R.id.result_show_timeCost);
		showAllCounts = (TextView) findViewById(R.id.result_show_wordsCount);
		showHitRate = (TextView) findViewById(R.id.result_show_hitRate);
		showRate = (TextView) findViewById(R.id.result_show_rate);
		wrongSet = (ListView) findViewById(R.id.result_show_wrongWordList);
		
		backToMenu.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(ResultShow.this, Main.class);
				finish();
			}
		});
		
		setTextOfComponents();
	}
	
	private void setTextOfComponents(){
		showAllTime.setText(getFormatTime(allTimeInMillis));
		showAllCounts.setText("共计：" + allCounts + "题");
		showHitRate.setText(getFormatRate(wrongCounts, allCounts));
		showRate.setText(getRate(ratePer));
	}
	
	private String getFormatRate(int wrong, int all){
		int correct = all - wrong;
		ratePer = (correct * 1.0 / allCounts) * 100;
		return "对：" + correct + " 错：" + wrong + " 正确率：" + String.format("%1$-4.2f", ratePer) + "%";
	}
	
	private String getRate(double rate){
		if(rate >= 95.0) return "A+";
		if(rate >= 90.0) return "A";
		if(rate >= 85.0) return "A-";
		if(rate >= 80.0) return "B+";
		if(rate >= 75.0) return "B";
		if(rate >= 70.0) return "B-";
		if(rate >= 65.0) return "C+";
		if(rate >= 60.0) return "C";
		if(rate < 60.0) return "F";
		return "F";
	}
	
	private String getFormatTime(long allTimeInMillis){
		long second = allTimeInMillis / 1000L;
		long minute = second / 60;
		second %= 60;
		return "耗时：" + minute + "分钟" + second + "秒";
	}
	
	private void prepareInBackground(){
		progressDialog = new ProgressDialog(ResultShow.this);
		progressDialog.setMessage(getText(R.string.tip_loading));
		progressDialog.show();
		
		new Thread(){
			public void run() {
				prepareCursorData();
				calculateAndSaveData();
				loadingHandler.sendEmptyMessage(0);
			};
		}.start();
	}
	
	private void prepareCursorData(){
		ActivityHelper helper = new ActivityHelper(ResultShow.this);
		helper.open();
		cursor = helper.getCursorDataOfWrongWords(latelyAccessTimeInMillis);
		adapter = new MyAdapter(ResultShow.this, cursor, false);
		helper.close();
	}
	
	private void calculateAndSaveData(){
		SharedPreferences pref = getSharedPreferences(CommandList.STATISTIC_DATA, MODE_PRIVATE);
		int allTestCounts = pref.getInt(CommandList.ALL_TEST_COUNTS, 0);
		int allWrongCounts = pref.getInt(CommandList.ALL_WRONG_COUNTS, 0);
		long allTimeCostInMillis = pref.getLong(CommandList.ALL_TIME_COST, 0L);
		
		allTestCounts += allCounts;
		allWrongCounts += wrongCounts;
		allTimeCostInMillis += allTimeInMillis;
		
		SharedPreferences.Editor editor = pref.edit();
		editor.putInt(CommandList.ALL_TEST_COUNTS, allTestCounts);
		editor.putInt(CommandList.ALL_WRONG_COUNTS, allWrongCounts);
		editor.putLong(CommandList.ALL_TIME_COST, allTimeCostInMillis);
		editor.commit();
	}

}
